package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/07 14:25
 */
@Service("comboCheckFieldAnalyseServiceImpl")
public class ComboCheckFieldAnalyseServiceImpl implements FieldAnalyseService {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO_CHECK.getType(), FieldTypeEnum.CHECKBOX_GROUP.getType());
    }

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntityExt, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        if (object instanceof String) {
            String value = String.valueOf(object);
            List<String> valueList;
            if (value.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = value.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else {
                valueList = Arrays.asList(value);
            }
            // 复选框/下拉复选
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), thisField.getComboType())) {
                object = ItemUtil.getCustomItemList(thisField, valueList);
            } else {
                object = valueList;
            }
        } else if (object instanceof List) {
            List<Object> itemList = JSON.parseArray(JSON.toJSONString(object), Object.class);
            List<String> valueList = new ArrayList<>();
            itemList.forEach(item->{
                if (item instanceof ItemPoJo || item instanceof Map) {
                    ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(item), ItemPoJo.class);
                    valueList.add(itemPoJo.getValue().toString());
                } else {
                    valueList.add(item.toString());
                }
            });
            if (Objects.equals(FieldTypeEnum.COMBO_CHECK.getType(), thisField.getFieldType()) && Objects.equals(ComboTypeEnum.CUSTOM.getType(), thisField.getComboType())) {
                object = ItemUtil.getCustomItemList(thisField, valueList);
            } else {
                object = valueList;
            }
        }
        return object;
    }
}
